<?php
/**
 * 商品推荐控制器
 * Created by PhpStorm.
 * User: Jmashimaro
 * Date: 2015/2/11
 * Time: 12:00
 */
class RecommendApp extends FrontendApp {

    /**
     * @var CommonApp
     */
    private $common;

    /**
     * 初始化
     */
    public function __construct() {
        $this->common = new CommonApp();
    }

    /**
     * 获取推荐列表
     * @author jiayanxiu
     * @date 2015/02/11
     */
    public function run() {
        $action = '获取推荐列表';
        setLogParameter($action);
        $method = strtoupper($_SERVER['REQUEST_METHOD']);
        if ($method !== 'GET') {
            $this->ajax_return(FLAG_ERROR, '非法的请求！');
        }
        $user_id = $this->checkLogin();
        $tmp = $data = $row = array();
        $type = $this->get_gp('type', 'G');
        if ($type !== 'star' && $type !== 'editor' && $type !== 'designer') {
            $type = 'editor';
        }
        $offset = $this->get_gp('offset', 'G');
        $size = $this->get_gp('size', 'G');
        $offset = $offset ? intval($offset) : 0;
        $size = $size ? intval($size) : 10;
        //获取推荐的信息
        $model = m('recommend');
        $sql = "SELECT recom_id FROM ".DB_PREFIX."recommend WHERE type='{$type}' ORDER BY sort_order DESC LIMIT {$offset},{$size}";
        $stars = $model->getAll($sql);
        if (!$stars) {
            $this->ajax_return(FLAG_ERROR, '此模块没有数据！');
        }
        //添加七牛路径
        $recom_id = '';
        foreach($stars as $k => $v) {
            $recom_id .= $v['recom_id'].',';
        }
        $recom_id = rtrim($recom_id, ',');
        //根据推荐的id获取推荐信息
        $sql = "SELECT recom_id,recom_name,recom_title,recom_content,type,recom_image,video_link
                FROM ".DB_PREFIX."recommend WHERE recom_id IN ({$recom_id}) ORDER BY sort_order DESC";
        $tmp = $model->getAll($sql);
        //获取推荐的信息
        if ($tmp) {
            foreach ($tmp as $key=>$val) {
                $row[$val['recom_id']]['recom_id'] = $val['recom_id'];
                $row[$val['recom_id']]['recom_name'] = $val['recom_name'];
                $row[$val['recom_id']]['recom_title'] = $val['recom_title'];
                $row[$val['recom_id']]['recom_content'] = $val['recom_content'];
                $row[$val['recom_id']]['type'] = $val['type'];
                $row[$val['recom_id']]['video_link'] = $val['video_link'];
                $row[$val['recom_id']]['recom_image'] = $val['recom_image'] ? QINIU_URL.$val['recom_image'].DISCOVERY : '';
            }
        }
        //获取推荐的商品的信息
        if ($row) {
            foreach ($row as $k=>$v) {
            $sql = "SELECT g.goods_id,g.store_id,g.goods_name,g.introduction,g.default_image,g.price,g.source,g.currency,g.is_act,
                g.discount_price,g.act_start_time,g.act_end_time,gs.collects,gs.base_collects FROM ".DB_PREFIX."goods g
                LEFT JOIN ".DB_PREFIX."goods_statistics gs ON g.goods_id=gs.goods_id
                WHERE g.goods_id IN (SELECT goods_id FROM ".DB_PREFIX."recommend_goods WHERE recom_id={$v['recom_id']})
                AND g.if_examine = 1 AND g.if_finance = 1 AND g.if_show = 1 AND g.closed = 0 GROUP BY g.goods_id
                ORDER BY g.last_update DESC LIMIT 10;";
                $info = $model->getAll($sql);
                if ($info) {
                    foreach ($info as $keys=>$value) {
                        $value['collects'] = $value['collects'] + $value['base_collects'];
                        $value['discount'] = $this->common->isAct($value['is_act'], $value['act_start_time'], $value['act_end_time']);
                        $value['follow'] = $user_id ? $this->common->getRelationShip($user_id, $value['goods_id']) : 0;//是否关注了此商品
                        $value['default_image'] = $this->common->isMySelfImage($value['default_image'], DISCOVERY_GOODS_IMG);
                        $info[$keys] = $value;
                    }
                }
                unset($v['goods_id']);
                $v['goods'] = $info;
                $data[] = $v;
            }
        }
        $this->ajax_return(FLAG_OK, '', $data);
    }


    /**
     * 单个推荐列表
     * @author jiayanxiu
     * @date 2015/02/01
     */
    public function singleList() {
        $action = '单个推荐列表';
        setLogParameter($action);
        $method = strtoupper($_SERVER['REQUEST_METHOD']);
        if ($method !== 'GET') {
            $this->ajax_return(FLAG_ERROR, '非法的请求！');
        }
        $recom_id = $this->get_gp('recom_id', 'G');
        $offset = $this->get_gp('offset', 'G');
        $size = $this->get_gp('size', 'G');
        $offset = $offset ? intval($offset) : 0;
        $size = $size ? intval($size) : 10;
        $recom_id = $recom_id ? intval($recom_id) : 0;
        if (!$recom_id) {
            $this->ajax_return(FLAG_ERROR, '推荐类型的id不存在！');
        }
        $user_id = $this->checkLogin();
        $model = m('recommend');
        $time = time();
        $sql = "SELECT recom_id,recom_name,recom_title,recom_content,recom_image,video_link FROM ".DB_PREFIX."recommend WHERE recom_id={$recom_id}";
        $data = $model->getRow($sql);
        if (!$data) {
            $this->ajax_return(FLAG_ERROR, '此信息不存在！');
        }
        $data['recom_image'] = $data['recom_image'] ? QINIU_URL.$data['recom_image'].DISCOVERY : '';
        $sql = "SELECT rg.recom_id,g.goods_id,g.goods_name,g.store_id,g.price,g.default_image,g.price,g.introduction,
            g.source,g.currency,g.is_act,g.discount_price,g.act_start_time,g.act_end_time,gs.collects,gs.base_collects
            FROM ".DB_PREFIX."recommend_goods rg LEFT JOIN ".DB_PREFIX."goods g ON rg.goods_id=g.goods_id
            LEFT JOIN ".DB_PREFIX."goods_statistics gs ON gs.goods_id=g.goods_id WHERE rg.recom_id={$recom_id}
            AND if_examine = 1 AND if_finance = 1 AND if_show = 1 AND closed = 0 ORDER BY rg.recom_id DESC LIMIT {$offset},{$size}";
        $list = $model->getAll($sql);
        if ($list) {
            foreach ($list as $key=>$val) {
                $val['follow'] = $user_id ? $this->common->getRelationShip($user_id, $val['goods_id']) : 0;//是否关注了此商品
                //查询出商品的库存
                $val['allstock'] = $this->common->getStock($val['goods_id']);
                $val['default_image'] = $this->common->isMySelfImage($val['default_image'], GOODS_IMG);
                $val['collects'] = intval($val['collects']) + intval($val['base_collects']);//两个数据的总和
                $val['discount'] = $this->common->isAct($val['is_act'], $val['act_start_time'], $val['act_end_time']);
                $list[$key] = $val;
            }
            $data['goods'] = $list;
            $this->ajax_return(FLAG_OK, '', $data);
        } else {
            $this->ajax_return(FLAG_ERROR, '没有更多数据！');
        }

    }

}